package com.mengxuegu.workflow;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

/**
 * 如果报错如下：
 * Cause: java.sql.SQLSyntaxErrorException: Unknown column 'VERSION_' in 'field list'
 * Cause: java.sql.SQLSyntaxErrorException: Unknown column 'PROJECT_RELEASE_VERSION_' in 'field
 *
 * 解决： ACT_RE_DEPLOYMENT 表缺少 VERSION_ 和 PROJECT_RELEASE_VERSION_ 字段，执行下面添加表字段
 * ALTER TABLE ACT_RE_DEPLOYMENT ADD COLUMN VERSION_ VARCHAR(255);
 * ALTER TABLE ACT_RE_DEPLOYMENT ADD COLUMN PROJECT_RELEASE_VERSION_ VARCHAR(255);
 */

@RunWith(SpringRunner.class)
@SpringBootTest
public class ActivitiTest01 {

    @Resource
    ProcessEngine processEngine;

    @Autowired
    RepositoryService repositoryService;

    /*** 获取 ProcessEngine 流程引擎，自动创建 activiti 数据表 */
    @Test
    public void getProcessEngine() {
        // 获取 Activiti 流程引擎 和 创建数据库表，并在 ACT_RE_DEPLOYMENT 插入一条的数据
        System.out.println(processEngine);
    }

    /*** 部署流程 */
    @Test
    public void deployByFile() {
        // 1. 创建部署对象进行流程的部署，定义一个流程的名字，把 .bpmn 和 .png 部署到数据库中
        Deployment deployment = repositoryService.createDeployment()
                .name("请假申请流程")
                .addClasspathResource("processes/leave.bpmn")
                .addClasspathResource("processes/leave.png")
                .deploy();
        // 2. 输出部署信息
        System.out.println("部署ID：" + deployment.getId());
        System.out.println("部署名称：" + deployment.getName());
    }
}
